package com.yc.qunxing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.qunxing.entity.DriverVehicles;
import com.yc.qunxing.entity.Drivers;
import com.yc.qunxing.entity.Users;
import com.yc.qunxing.mapper.DriverVehiclesMapper;
import com.yc.qunxing.mapper.DriversMapper;
import com.yc.qunxing.result.Result;
import com.yc.qunxing.service.IDriversService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 鹏哥
 * @since 2024-05-23
 */
@Service
public class DriversServiceImpl extends ServiceImpl<DriversMapper, Drivers> implements IDriversService {

    @Autowired
    private DriverVehiclesMapper driverVehiclesMapper;

    @Autowired
    private DriversMapper driversMapper;
    @Override
    @Transactional
    public Result<String> registerDriver(Drivers drivers) {
        // 插入司机信息并获取自动生成的id值
        drivers.setStatus("4");
        driversMapper.insertAndGetId(drivers);
        System.out.println(drivers+"9999");
        // 保存司机车辆信息
        DriverVehicles driverVehicles = drivers.getDriverVehicles();
        driverVehicles.setDriverId(drivers.getId()); // 设置外键关联
        driverVehiclesMapper.insert(driverVehicles);

        return Result.success("注册成功");
    }
    @Override
    public boolean verifyOriginalPassword(Integer driverId, String originalPassword) {
        Drivers driver = driversMapper.selectById(driverId);
        return driver != null && driver.getPassword().equals(originalPassword);
    }

    @Override
    public boolean changePassword(Integer driverId, String newPassword) {
        Drivers driver=new Drivers();
        driver.setId(driverId);
        driver.setPassword(newPassword);
        return driversMapper.updateById(driver) == 1;
    }
    @Override
    public boolean changePhone(Integer driverId, String phone) {
        Drivers driver=new Drivers();
        driver.setId(driverId);
        driver.setPhoneNumber(phone);
        return driversMapper.updateById(driver) == 1;
    }

    @Override
    public boolean changeName(Integer driverId, String name) {
        Drivers driver=new Drivers();
        driver.setId(driverId);
        driver.setUsername(name);
        return driversMapper.updateById(driver) == 1;
    }
    @Resource
    private JavaMailSender mailSender;

    @Override
    public boolean sendVerificationCode(Integer driverId, String email, String code) {
        String to=email;
        String subject="群星网约车官方验证码";
        String content=code;
        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom("936482013@qq.com");
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
        System.out.println("Sending verification code " + code + " to email " + email);
        return true;
    }
    @Override
    public boolean changeEmail(Integer driverId, String newEmail) {
        Drivers driver=new Drivers();
        driver.setId(driverId);
        driver.setEmail(newEmail);
        return driversMapper.updateById(driver) == 1;
    }

    @Override
    public List<Drivers> getDriverByManager(String keyword, String keywordOrder,String searchVehicle) {
        return driversMapper.getDriverByManager(keyword, keywordOrder, searchVehicle);
    }
}
